package com.controller;

import java.io.File;


import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.KeHu;
import com.entity.Sysuser;
import com.server.KeHuServer;
import com.server.SysuserServier;
import com.server.RecordServer;
import com.util.PageBean;

@Controller
public class KeHuController {
	@Resource
	private KeHuServer keHuService;
	@Resource
	private RecordServer recordService;
	@Resource
	private SysuserServier userService;
	
	@RequestMapping("admin/doAddKeHu.do")
	public String doAddKeHu(ModelMap map){
		map.put("ulist", userService.getAll(null));
		return "admin/add_KeHu";
	}
//	文件上传
	public String fileUpload(@RequestParam(value="file",required=false)MultipartFile file,
			HttpServletRequest request,String img){
		String path=request.getSession().getServletContext().getRealPath("upload");
		String fileName=file.getOriginalFilename();
		File targetFile=new File(path,fileName);
		if(!targetFile.exists()){
			targetFile.mkdirs();
		}
		try {
			file.transferTo(targetFile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String pa=request.getContextPath()+"/upload/"+fileName;
		System.out.println("path==="+pa);
		if(fileName!=null&&!fileName.equals("")){
			img=fileName;
		}else{
			img=null;	
		}
		return img;
		
	}
	@RequestMapping("admin/addKeHu.do")
	public String addKeHu(@RequestParam(value="file",required=false)MultipartFile file,
			HttpServletRequest request,KeHu KeHu,HttpSession session,String img){
		System.out.println("ddddd");
		Sysuser u=(Sysuser)session.getAttribute("auser");
		System.out.println("u=-="+u);
		if(u==null||u.equals("")){
			return "admin/nologin";
		}else{
		img=fileUpload(file, request, img);
		KeHu.setImg(img);
		java.sql.Timestamp time=new Timestamp(System.currentTimeMillis());
		KeHu.setPubtime(time.toString().substring(0, 19));
		KeHu.setIsdel("1");
		KeHu.setUid(u.getUid()+"");
		keHuService.add(KeHu);
		return "admin/success";
		}
	}
	@RequestMapping("admin/doUpdateKeHu.do")
	public String doUpdateKeHu(ModelMap map,int id){
		map.put("jz", keHuService.getById(id));
		map.put("ulist", userService.getAll(null));
		return "admin/update_kehu";
	}
	@RequestMapping("admin/updateKeHu.do")
	public String updateKeHu(@RequestParam(value="file",required=false)MultipartFile file,
			HttpServletRequest request,KeHu KeHu,String img){
		img=fileUpload(file, request, img);
		if(img==null){
			KeHu kh=keHuService.getById(KeHu.getId());
			img=kh.getImg();
		}
		KeHu.setImg(img);
		keHuService.update(KeHu);
		return "admin/success";
	}
	

//	分页查询
	@RequestMapping("admin/KeHuList.do")
	public String KeHuList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session){
		Sysuser u=(Sysuser)session.getAttribute("auser");
		if(u==null){
			return "admin/nologin";
		}else{
		System.out.println("dddd=====");
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), PageBean.PAGESIZE);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());
		Map<String, Object> cmap=new HashMap<String, Object>();
		if(u.getUtype().equals("用户")){
			pmap.put("uid", u.getUid());
			cmap.put("uid", u.getUid());
		}else{
			pmap.put("uid", null);
			cmap.put("uid", null);
		}
		pmap.put("area", "潜在客户");
		cmap.put("area", "潜在客户");
		int total=keHuService.getCount(cmap);
		pageBean.setTotal(total);
		List<KeHu> list=keHuService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		map.put("ulist", userService.getAll(null));
		session.setAttribute("p", 1);
		return "admin/list_kehu";
		}
	}
	@RequestMapping("admin/KeHuList2.do")
	public String KeHuList2(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session){
		Sysuser u=(Sysuser)session.getAttribute("auser");
		if(u==null){
			return "admin/nologin";
		}else{
		System.out.println("dddd=====");
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), PageBean.PAGESIZE);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());
		Map<String, Object> cmap=new HashMap<String, Object>();
		if(u.getUtype().equals("用户")){
			pmap.put("uid", u.getUid());
			cmap.put("uid", u.getUid());
		}else{
			pmap.put("uid", null);
			cmap.put("uid", null);
		}
		pmap.put("area", "正式客户");
		cmap.put("area", "正式客户");
		int total=keHuService.getCount(cmap);
		pageBean.setTotal(total);
		List<KeHu> list=keHuService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		map.put("ulist", userService.getAll(null));
		session.setAttribute("p", 1);
		return "admin/list_kehu2";
		}
	}
//   分页模糊查询
	@RequestMapping("admin/vagueKeHuList.do")
	public String vagueKeHuList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,KeHu cd){
		Sysuser u=(Sysuser)session.getAttribute("auser");
		if(u==null){
			return "admin/nologin";
		}else{
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page),PageBean.PAGESIZE);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());
		
		Map<String, Object> cmap=new HashMap<String,Object>();
       if(cd.getName()!=null&&!cd.getName().equals("")){
			cmap.put("name", cd.getName());
			pmap.put("name", cd.getName());
		}
       if(u.getUtype().equals("用户")){
			pmap.put("uid", u.getUid());
			cmap.put("uid", u.getUid());
		}else{
			pmap.put("uid", null);
			cmap.put("uid", null);
		}
        pmap.put("area", "潜在客户");
		cmap.put("area", "潜在客户");
		int total=keHuService.getCount(cmap);
		pageBean.setTotal(total);
		List<KeHu> list=keHuService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		map.put("ulist", userService.getAll(null));
		session.setAttribute("p", 2);
		return "admin/list_kehu";
		}
	}
	
	@RequestMapping("admin/vagueKeHuList2.do")
	public String vagueKeHuList2(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,KeHu cd){
		Sysuser u=(Sysuser)session.getAttribute("auser");
		if(u==null){
			return "admin/nologin";
		}else{
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page),PageBean.PAGESIZE);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());
		
		Map<String, Object> cmap=new HashMap<String,Object>();
       if(cd.getName()!=null&&!cd.getName().equals("")){
			cmap.put("name", cd.getName());
			pmap.put("name", cd.getName());
		}
       if(u.getUtype().equals("用户")){
			pmap.put("uid", u.getUid());
			cmap.put("uid", u.getUid());
		}else{
			pmap.put("uid", null);
			cmap.put("uid", null);
		}
        pmap.put("area", "正式客户");
		cmap.put("area", "正式客户");
		int total=keHuService.getCount(cmap);
		pageBean.setTotal(total);
		List<KeHu> list=keHuService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		map.put("ulist", userService.getAll(null));
		session.setAttribute("p", 2);
		return "admin/list_kehu2";
		}
	}
	

	@RequestMapping("admin/deleteKeHu.do")
	public String deleteKeHu(int id){
		keHuService.delete(id);
		return "admin/success";
	}
}
